package NetWork;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {

    private Socket socket = null;

    public TcpClient(String ServerIp,int ServerPort) throws IOException {
        socket = new Socket(ServerIp,ServerPort);  //把服务器的ip和端口告知网卡，，
    }

    public void start() throws IOException {
        System.out.println("客户端启动:->");
try ( InputStream inputStream = socket.getInputStream();
      OutputStream outputStream = socket.getOutputStream();){

    while(true) {
        Scanner sc = new Scanner(System.in);
        String request = sc.next();
        if(request.equals("exit")){
            break;
        }
        //这里就是把数据给服务器发过去的操作
        PrintWriter printWriter = new PrintWriter(outputStream);
        printWriter.println(request);
        printWriter.flush();

        //需要接受服务器的响应

        Scanner scanner = new Scanner(inputStream);
        String repose = scanner.next();
        System.out.println(repose);
    }
}catch (IOException e){
    e.printStackTrace();
}


    }

    public static void main(String[] args) throws IOException {
        TcpClient tcpClient = new TcpClient("127.0.0.1",9090);
        tcpClient.start();
    }

}
